// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/exec/round_robin_partition_function.h>
#include <gtest/gtest.h>
#include <pollux/vector/vector_maker.h>
#include <pollux/testing/vector/vector_test_base.h>

using namespace kumo;
using namespace kumo::pollux;
using namespace kumo::pollux::exec;

class RoundRobinPartitionFunctionTest : public pollux::VectorBuilder,
                                        public testing::Test {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }
};

TEST_F(RoundRobinPartitionFunctionTest, basic) {
  exec::RoundRobinPartitionFunction partitionFunction(10);

  auto pool = memory::memoryManager()->addLeafPool();
  pollux::VectorMaker vm(pool.get());

  auto data = vm.row_vector(ROW({}, {}), 1024);
  std::vector<uint32_t> partitions;
  for (auto i = 0; i < 31; ++i) {
    SCOPED_TRACE(i);
    auto partition = partitionFunction.partition(*data, partitions);
    ASSERT_TRUE(partition.has_value());
    ASSERT_EQ(i % 10, partition.value());
    ASSERT_TRUE(partitions.empty());
  }
}

TEST_F(RoundRobinPartitionFunctionTest, spec) {
  auto roundRobinSpec = std::make_unique<RoundRobinPartitionFunctionSpec>();
  auto serialized = roundRobinSpec->serialize();
  auto copy = RoundRobinPartitionFunctionSpec::deserialize(serialized, pool());
  ASSERT_EQ(roundRobinSpec->toString(), copy->toString());
}
